#!/bin/bash

# ===============================================
# Raspberry Pi Toolchains(raspberry-pi-cross-compilers): This project
# provides latest automated GCC Cross Compiler & Native (ARM & ARM64)
# build-scripts and precompiled standalone toolchains for Raspberry Pi.

# Copyright (C) 2020 Abhishek Thakur(@abhiTronix) <abhi.una12@gmail.com>

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
# ===============================================

# This script auto-deploys Toolchain specific README.md to Sourceforge secured server

helpfunction() {
    #helper function that prints custom usage help menu
    echo ""
    echo ""
    figlet -t -k -f /usr/share/figlet/standard.flf "Raspberry Pi Toolchains Docs Deployer"
    echo ""
    figlet -t -k -f /usr/share/figlet/term.flf "Copyright (c) 2020 abhiTronix"
    echo ""
    echo ""
    echo "Usage: $0 -t [Toolchain Type] -f [Folder type] -o [Target Pi OS type] -r [Raspberry Pi type] "
    echo -e "\t-t Toolchain Type?: (cross|native|cross_64|native_64|CLI)"
    echo -e "\t-f Upload Folder type?: (root|main|base|experimental)"
    echo -e "\t-o Raspberry Pi OS type?: (stretch|buster)"
    echo -e "\t-r What's yours Target Raspberry Pi type?: (0-1|2-3|3+|x86|x86_64)"
    echo ""
    echo ""
    exit 1 # Exit script after printing help
}

#input arguments handler
while getopts "t:f:o:r:" opt; do
    case "$opt" in
    t) TOOLCHAIN="$OPTARG" ;;
    f) FOLDER_TYPE="$OPTARG" ;;
    o) RPIOS_TYPE="$OPTARG" ;;
    r) RPI_TYPE="$OPTARG" ;;
    ?) helpfunction ;; #prints help function for invalid parameter
    esac
done

#validates parameters and print usage helper function in case parameters are missing
if [ -z "$TOOLCHAIN" ]; then
    TOOLCHAIN="others"
fi

if [ -z "$FOLDER_TYPE" ]; then
    echo "Error: Required parameters are missing!"
    helpfunction
fi

if ! [[ "$FOLDER_TYPE" =~ ^("root"|"main"|"base"|"experimental")$ ]]; then
    echo "Error: Wrong folder!"
    exit 1
fi

echo "Doing $TOOLCHAIN with Folder: $FOLDER_TYPE, RASPBERRY: $RPI_TYPE AND OS: $RPIOS_TYPE!"

# deploy root folder docs
if [ "$FOLDER_TYPE" = "root" ]; then
    if [ -f "$HOME/root/README.md" ]; then
        curl -k "sftp://frs.sourceforge.net/home/pfs/project/raspberry-pi-cross-compilers/" -u "$USER_SFTP":"$PASSWORD_SFTP" -T "$HOME/root/README.md" --ftp-create-dirs
    else
        echo "Error: No valid Root docs found!"
        exit 1
    fi
    exit 0
fi

if [ "$FOLDER_TYPE" = "base" ] && [ -f "$HOME/base/README.md" ]; then
    if [ "$TOOLCHAIN" = "CLI" ]; then
        FOLDER="CLI Base"
        curl -k "sftp://frs.sourceforge.net/home/pfs/project/raspberry-pi-cross-compilers/$FOLDER/" -u "$USER_SFTP":"$PASSWORD_SFTP" -T "$HOME/base/README.md" --ftp-create-dirs
    elif [[ "$TOOLCHAIN" =~ ^("cross_64"|"native_64")$ ]]; then
        FOLDER="Bonus Raspberry Pi GCC 64-Bit Toolchains"
        curl -k "sftp://frs.sourceforge.net/home/pfs/project/raspberry-pi-cross-compilers/$FOLDER/" -u "$USER_SFTP":"$PASSWORD_SFTP" -T "$HOME/base/README.md" --ftp-create-dirs
    else
        if [ "$TOOLCHAIN" = "cross" ]; then
            FOLDER="Raspberry Pi GCC Cross-Compiler Toolchains"
            curl -k "sftp://frs.sourceforge.net/home/pfs/project/raspberry-pi-cross-compilers/$FOLDER/" -u "$USER_SFTP":"$PASSWORD_SFTP" -T "$HOME/base/README.md" --ftp-create-dirs
        elif [ "$TOOLCHAIN" = "native" ]; then
            FOLDER="Raspberry Pi GCC Native-Compiler Toolchains"
            curl -k "sftp://frs.sourceforge.net/home/pfs/project/raspberry-pi-cross-compilers/$FOLDER/" -u "$USER_SFTP":"$PASSWORD_SFTP" -T "$HOME/base/README.md" --ftp-create-dirs
        else
            echo "Error: Invalid Raspberry Compiler Type!"
            exit 0
        fi
    fi
    exit 0
fi

# deploy Toolchain specific folder docs
if [ "$FOLDER_TYPE" = "main" ] && [ -f "$HOME/main/README.md" ]; then
    if [ "$TOOLCHAIN" = "cross" ]; then
        if [ "$RPIOS_TYPE" = "buster" ]; then
            FOLDER="Raspberry Pi GCC Cross-Compiler Toolchains/Buster"
            curl -k "sftp://frs.sourceforge.net/home/pfs/project/raspberry-pi-cross-compilers/$FOLDER/" -u "$USER_SFTP":"$PASSWORD_SFTP" -T "$HOME/main/README.md" --ftp-create-dirs
        elif [ "$RPIOS_TYPE" = "stretch" ]; then
            FOLDER="Raspberry Pi GCC Cross-Compiler Toolchains/Stretch"
            curl -k "sftp://frs.sourceforge.net/home/pfs/project/raspberry-pi-cross-compilers/$FOLDER/" -u "$USER_SFTP":"$PASSWORD_SFTP" -T "$HOME/main/README.md" --ftp-create-dirs
        else
            echo "Error: Invalid Raspberry OS Type!"
            exit 1
        fi
    elif [ "$TOOLCHAIN" = "native" ]; then
        if [ "$RPIOS_TYPE" = "buster" ]; then
            FOLDER="Raspberry Pi GCC Native-Compiler Toolchains/Buster"
            curl -k "sftp://frs.sourceforge.net/home/pfs/project/raspberry-pi-cross-compilers/$FOLDER/" -u "$USER_SFTP":"$PASSWORD_SFTP" -T "$HOME/main/README.md" --ftp-create-dirs
        elif [ "$RPIOS_TYPE" = "stretch" ]; then
            FOLDER="Raspberry Pi GCC Native-Compiler Toolchains/Stretch"
            curl -k "sftp://frs.sourceforge.net/home/pfs/project/raspberry-pi-cross-compilers/$FOLDER/" -u "$USER_SFTP":"$PASSWORD_SFTP" -T "$HOME/main/README.md" --ftp-create-dirs
        else
            echo "Error: Invalid Raspberry OS Type!"
            exit 0
        fi
    elif [ "$TOOLCHAIN" = "cross_64" ]; then
        if [ "$RPIOS_TYPE" = "buster" ]; then
            FOLDER="Bonus Raspberry Pi GCC 64-Bit Toolchains/Raspberry Pi GCC 64-Bit Cross-Compiler Toolchains/Buster"
        else
            FOLDER="Bonus Raspberry Pi GCC 64-Bit Toolchains/Raspberry Pi GCC 64-Bit Cross-Compiler Toolchains/Stretch"
        fi
        curl -k "sftp://frs.sourceforge.net/home/pfs/project/raspberry-pi-cross-compilers/$FOLDER/" -u "$USER_SFTP":"$PASSWORD_SFTP" -T "$HOME/main/README.md" --ftp-create-dirs
    elif [ "$TOOLCHAIN" = "native_64" ]; then
        if [ "$RPIOS_TYPE" = "buster" ]; then
            FOLDER="Bonus Raspberry Pi GCC 64-Bit Toolchains/Raspberry Pi GCC 64-Bit Native-Compiler Toolchains/Buster"
        else
            FOLDER="Bonus Raspberry Pi GCC 64-Bit Toolchains/Raspberry Pi GCC 64-Bit Native-Compiler Toolchains/Stretch"
        fi
        curl -k "sftp://frs.sourceforge.net/home/pfs/project/raspberry-pi-cross-compilers/$FOLDER/" -u "$USER_SFTP":"$PASSWORD_SFTP" -T "$HOME/main/README.md" --ftp-create-dirs
    else
        echo "Error: Invalid Main toolchain!"
        exit 1
    fi
    exit 0
fi

if [ "$FOLDER_TYPE" = "experimental" ]; then
    if [ -f "$HOME/experiment/README.md" ] && [[ "$RPI_TYPE" =~ ^("x86"|"x86_64")$ ]]; then
        FOLDER="Exclusive-Experimental Toolchains/Desktop/$RPI_TYPE"
        curl -k "sftp://frs.sourceforge.net/home/pfs/project/raspberry-pi-cross-compilers/$FOLDER/" -u "$USER_SFTP":"$PASSWORD_SFTP" -T "$HOME/experiment/README.md" --ftp-create-dirs
    elif [ -f "$HOME/main/README.md" ]; then
        FOLDER="Exclusive-Experimental Toolchains/Desktop"
        curl -k "sftp://frs.sourceforge.net/home/pfs/project/raspberry-pi-cross-compilers/$FOLDER/" -u "$USER_SFTP":"$PASSWORD_SFTP" -T "$HOME/main/README.md" --ftp-create-dirs
    elif [ -f "$HOME/base/README.md" ]; then
        FOLDER="Exclusive-Experimental Toolchains"
        curl -k "sftp://frs.sourceforge.net/home/pfs/project/raspberry-pi-cross-compilers/$FOLDER/" -u "$USER_SFTP":"$PASSWORD_SFTP" -T "$HOME/base/README.md" --ftp-create-dirs

    else
        echo "Warning: No Experimental docs found!"
        exit 1
    fi
fi
